#include "parentwidget.h"
#include "QResizeEvent"
#include <QEvent>
#include "Windows.h"
#include "QVariant"
#include "ui_parentwidget.h"

#include <QWindow>

ParentWidget::ParentWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParentWidget),
    childWidget(NULL)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    showFullScreen();
    connect(ui->integrateWidgetBtn, SIGNAL(clicked(bool)), this, SLOT(integrate()));

}

ParentWidget::~ParentWidget()
{
    delete ui;
}

void ParentWidget::integrate()
{


    HWND AppWnd;
  //  LPCWSTR App = reinterpret_cast <LPCWSTR>(className.data());
  //  LPCWSTR AppCaption = reinterpret_cast <LPCWSTR>(appName.data());
    AppWnd= ::FindWindow(L"UnrealWindow", L"QT (64-bit Development PCD3D_SM5) ");
    AppWnd1 =AppWnd;
    childWindow = QWindow::fromWinId((WId)AppWnd);
    QWindow* proc1Widow = QWindow::fromWinId(winId());
    //childWindow->setProperty("_q_embedded_native_parent_handle",winId());
   // childWindow->setParent(proc1Widow);
    //setMouseTracking(true);


    childWindow->setMouseGrabEnabled(true);
    childWindow->setKeyboardGrabEnabled(true);


    childWidget = QWidget::createWindowContainer(childWindow, this, Qt::Widget );
    //childWidget->setFocusPolicy(Qt::StrongFocus);
    //childWidget->setFocus();
    childWidget->grabKeyboard();
    //childWidget->grabMouse();

   // childWidget->setMouseTracking(true);
    //childWidget->mouseGrabber();
   // childWidget->keyboardGrabber();
    childWidget->resize(800,600);

    //this->setFocusPolicy(Qt::ClickFocus);
    ui->verticalLayout_2->addWidget(childWidget);

}

void ParentWidget::mousePressEvent(QMouseEvent * e)
{
    //获取点击的下标
    //childWidget->mou
    // int  key = Qt::Key_Space;
    //QKeyEvent EventPress(QEvent::KeyPress,key,Qt::NoModifier);
   // HWND AppWnd;
  //  LPCWSTR App = reinterpret_cast <LPCWSTR>(className.data());
  //  LPCWSTR AppCaption = reinterpret_cast <LPCWSTR>(appName.data());
  //  AppWnd= ::FindWindow(L"UnrealWindow", L"QT (64-bit Development PCD3D_SM5) ");
    //::PostMessage((HWND)childWidget->winId(), WM_KEYDOWN, VK_SPACE, 1);
      //  QApplication::sendEvent(childWidget,&EventPress);   //发送键盘按下事件
     //QCoreApplication::postEvent(childWindow, e);
    //qDebug() << e->x() << ":" << e->y();
    if(e->button() == Qt::LeftButton)
    {
        qDebug() << "左键" ;
    }
    else if(e->button() == Qt::RightButton)
    {
        qDebug() << "右键" ;
    }
}

void ParentWidget::keyPressEvent(QKeyEvent * e)
{

    ::PostMessage(AppWnd1, e->type(),  e->key(), 1);
}

void ParentWidget::on_pushButton_clicked()
{
   ::PostMessage(AppWnd1, WM_KEYDOWN, VK_SPACE, 1);
   //BringWindowToTop(AppWnd1);

}

void ParentWidget::on_pushButton_released()
{
   //  childWindow->focusObject();
}
